package net.lcheng.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import net.lcheng.commons.utils.PageList;
import net.lcheng.dao.PCategoryMapper;
import net.lcheng.model.PCategory;
import net.lcheng.model.PCategoryExample;
import net.lcheng.service.CategoryService;
import net.lcheng.utils.IdGenerator;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Created by zwr on 2016/6/27 0027.
 */
@Service
public class CategoryServiceImpl implements CategoryService {
    @Resource
    private PCategoryMapper mapper;

    @Override
    public PageList<PCategory> getCategoryByStatus(int pageIndex, int pageSize) {
        PageHelper.startPage(pageIndex,pageSize);
        PCategoryExample example = new PCategoryExample();
        List<PCategory> list = mapper.selectByExample(example);
        PageInfo<PCategory> pageInfo = new PageInfo<>(list);
        PageList<PCategory> pageList = new PageList<>();
        pageList.list = pageInfo.getList();
        pageList.totalCount = pageInfo.getTotal();
        pageList.totalPage=pageInfo.getPages();
        pageList.pageIndex=pageInfo.getPageNum();
        pageList.pageSize=pageInfo.getPageSize();
        return pageList;
    }

    /**
     * 添加商品分类
     * @param categoryName
     * @param categoryImage
     * @param topCategoryId 一级父类id
     * @param secondCategoryId 二级父类id
     * @param orderNum
     * @param summary
     * @return zwr
     */
    @Override
    public int insert( String categoryName, String categoryImage,String topCategoryId,String secondCategoryId,Integer orderNum, String summary) {
        PCategory model = new PCategory();
        model.setCategoryId(UUID.randomUUID().toString());
        model.setCategoryName(categoryName);
        model.setCategoryImage(categoryImage);
        int depth = 1;
        String parentCategoryId = "";//直接上级
        /**判断depth值*/
        if(com.alibaba.druid.util.StringUtils.isEmpty(topCategoryId)){
            depth = 1;
            parentCategoryId = "";
        }else {
            if(com.alibaba.druid.util.StringUtils.isEmpty(secondCategoryId)){
                depth = 2;
                parentCategoryId = topCategoryId;
            }else {
                depth = 3;
                parentCategoryId = secondCategoryId;
            }

        }
        model.setParentCategoryId(parentCategoryId);
        model.setDepth(depth);
        model.setStatus(1);
        model.setCategoryCode(IdGenerator.generateNumber());
        model.setCreateOn(new Date());
        model.setModifyOn(new Date());
        model.setOrderNum(orderNum);
        model.setSummary(summary);
        model.setTopCategoryId(topCategoryId);
        model.setSecondCategoryId(secondCategoryId);
        
        return mapper.insert(model);
    }

    @Override
    public PCategory getByCategoryId(String id) {
        return mapper.selectByPrimaryKey(id);
    }
    @Override
    public int modify(String categoryId,String categoryName, String categoryImage, String topCategoryId, Integer orderNum, String secondCategoryId, String summary) {
        PCategory model = new PCategory();
        model.setCategoryId(categoryId);
        model.setCategoryName(categoryName);
        model.setCategoryImage(categoryImage);
        int depth = 1;
        String parentCategoryId = "";//直接上级
        /**判断depth值*/
        if(com.alibaba.druid.util.StringUtils.isEmpty(topCategoryId)){
            depth = 1;
            parentCategoryId = "";
        }else {
            if(com.alibaba.druid.util.StringUtils.isEmpty(secondCategoryId)){
                depth = 2;
                parentCategoryId = topCategoryId;
            }else {
                depth = 3;
                parentCategoryId = secondCategoryId;
            }

        }
        model.setParentCategoryId(parentCategoryId);
        model.setDepth(depth);
        model.setStatus(1);
        model.setCategoryCode(IdGenerator.generateNumber());
        model.setCreateOn(new Date());
        model.setModifyOn(new Date());
        model.setOrderNum(orderNum);
        model.setSummary(summary);
        model.setTopCategoryId(topCategoryId);
        model.setSecondCategoryId(secondCategoryId);
        return mapper.updateByPrimaryKeySelective(model);
    }

    /**
     * 获取父分类名(包含自己)（by LV）
     * @return
     */
    @Override
    public List<String> getParentCategoryName(String categoryId) {
        List<String> categoryName=new ArrayList<>();
        //查询当前分类信息
        PCategory pCategory=this.getByCategoryId(categoryId);
        categoryName.add(pCategory.getCategoryName());

        for (;pCategory!=null&&!pCategory.getParentCategoryId().equals("");){
            pCategory=this.getByCategoryId(pCategory.getParentCategoryId());
            categoryName.add(pCategory.getCategoryName());
        }
        return categoryName;
    }


    @Override
    public int updateStatus(String Id, int Status) {
        PCategory model = new PCategory();
        model.setCategoryId(Id);
        model.setStatus(Status);
        return mapper.updateByPrimaryKeySelective(model);
    }

    @Override
    public List<PCategory> getChildCategoryByParentCategoryId(String parentCategoryId) {
        PCategoryExample example = new PCategoryExample();
        if(com.alibaba.druid.util.StringUtils.isEmpty(parentCategoryId)){
            example.or().andParentCategoryIdIsNull();
            example.or().andParentCategoryIdEqualTo("");
        }else
            example.or().andCategoryIdEqualTo(parentCategoryId);
        return mapper.selectByExample(example);
    }

    @Override
    public List<PCategory> getCategoryId(String categoryId) {
        PCategoryExample example = new PCategoryExample();
        example.or().andParentCategoryIdEqualTo(categoryId);
        return mapper.selectByExample(example);
    }


    /**
     * 由上级的CategoryId查找当前分类下的子列表（by LV）
     * @param parentCategoryId
     * @return
     */
    @Override
    public List<PCategory> findCategoryListByParentCategoryId(String parentCategoryId) {
        PCategoryExample example=new PCategoryExample();
        example.createCriteria().andParentCategoryIdEqualTo(parentCategoryId).andStatusEqualTo(1);
        example.setOrderByClause("order_num DESC");
        List<PCategory> categoryList=mapper.selectByExample(example);
        if (categoryList!=null&&categoryList.size()>0)
            return categoryList;
        return null;
    }


}
